package com.AStarExample
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class GridView extends Sprite
	{
		private var cellSize:int = 16;
		private var row:int = 50;
		private var col:int = 50;
		private var grid:Grid;
		private var aStar:AStar;
		private var pathArr:Array;
		public function GridView()
		{
			super();
			init()
		}
		
		private function init():void
		{
			this.grid = new Grid(this.row,this.col);
			//this.grid.setEndNode(20,20);
			this.grid.setStartNode(0,0);
			for(var i:int = 0;i<800;i++)
			{
				var $x:int = Math.floor(Math.random()*50);
				var $y:int = Math.floor(Math.random()*50);
				this.grid.getNode($x,$y).walkable = false;
			}
			
			drawGrid();
			//findPath();
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
			
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			var pox:int = Math.floor(e.localX / 16);
			var poy:int = Math.floor(e.localY / 16);
			if(!this.grid.getNode(pox,poy).walkable)return;
			this.grid.setEndNode(pox,poy);
			drawGrid();
			findPath();
			if(!this.pathArr)return;
			if(!this.hasEventListener(Event.ENTER_FRAME))this.addEventListener(Event.ENTER_FRAME,haha);
		}
		
		private function haha(e:Event):void
		{
			if(this.pathArr.length == 0)
			{
				this.removeEventListener(Event.ENTER_FRAME,haha);
				return;
			}
			var node :Node = this.pathArr.shift();
			this.grid.setStartNode(node.x,node.y);
			drawGrid();
			for(var i:int = 0;i<pathArr.length-1;i++)
			{
				node = pathArr[i] as Node;
				var color:uint = getColor(node);
				this.graphics.lineStyle(1,0);
				this.graphics.beginFill(0x00ffff);
				this.graphics.drawRect(node.x*this.cellSize,node.y*this.cellSize,this.cellSize,this.cellSize);
				this.graphics.endFill()
			}
		}
		
		private function drawGrid():void
		{
			this.graphics.clear();
			for(var i:int = 0;i<this.grid.rowNum;i++)
			{
				for(var j:int = 0;j<this.grid.colNum;j++)
				{
					var node:Node = this.grid.getNode(i,j);
					var color:uint = getColor(node);
					this.graphics.lineStyle(1,0);
					this.graphics.beginFill(color);
					this.graphics.drawRect(node.x*this.cellSize,node.y*this.cellSize,this.cellSize,this.cellSize);
					this.graphics.endFill()
				}
			}
		}
		
		private function findPath():void
		{
			this.aStar = new AStar();
			var isPath:Boolean = this.aStar.findPath(this.grid);
			if(isPath)
			{
				pathArr = this.aStar.getPath();
				for(var i:int = 0;i<pathArr.length-1;i++)
				{
					var node:Node = pathArr[i] as Node;
					var color:uint = getColor(node);
					this.graphics.lineStyle(1,0);
					this.graphics.beginFill(0x00ffff);
					this.graphics.drawRect(node.x*this.cellSize,node.y*this.cellSize,this.cellSize,this.cellSize);
					this.graphics.endFill()
				}
			}
		}
		
		private function getColor(node:Node):uint
		{
			if(!node.walkable)return 0;
			if(node == this.grid.startNode )return 0xff0000;
			if(node == this.grid.endNode)
			{
				return 0x00ff00;
			}
			return 0xffffff;
		}
	}
}